package com.ebupt.migu.music.global.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * txt文本解析
 *
 * @author wangzhu
 * @since 2021-12-31
 */
@Slf4j
public class TxtUtil {
    private static TxtUtil txtUtil = new TxtUtil();

    private TxtUtil() {
    }

    public static TxtUtil getTxtUtil() {
        return txtUtil;
    }

    public List toList(File file) {
        ArrayList list = new ArrayList<>();
        String str = null;
        try {
            log.info("读取文件路径:{}",file);
            //构造一个BufferedReader类来读取文件
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            //使用readLine方法，一次读一行
            while ((str = bufferedReader.readLine()) != null) {
                list.add(str);
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文本解析异常:{}", e);
        }
        log.info("文本解析数据围为:{}",list);
        return list;
    }


}
